/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.controllers;

import com.insurance.cis.daos.CampaignDao;
import com.insurance.cis.daos.ChannelDao;
import com.insurance.cis.daos.ProductDao;
import com.insurance.cis.daos.PromotionDao;
import com.insurance.cis.models.Campaign;
import com.insurance.cis.models.Channel;
import com.insurance.cis.models.Product;
import com.insurance.cis.models.Promotion;
import com.insurance.cis.utils.CisConstants;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.utils.Utils;
import com.insurance.cis.views.CampaignJDialogUpdateView;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author TruongLQ
 */
public class CampaignUpdateController {
    
    final CampaignJDialogUpdateView dialogUpdateView;
    final Pattern pattern = Pattern.compile(CisConstants.DATE_PATTERN_COMPILE);
    CampaignDao campaignDao = new CampaignDao();
    ProductDao productDao = new ProductDao();
    ChannelDao channelDao = new ChannelDao();
    PromotionDao promotionDao = new PromotionDao();
    
    public CampaignUpdateController(CampaignJDialogUpdateView aThis) {
        dialogUpdateView = aThis;
    }
    public Campaign validateCampaignInfos() {
        String campaignID = dialogUpdateView.getTxtCampaignID().getText();
        
        String channelID = dialogUpdateView.getTxtChannelID().getText();
        if (channelID.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogUpdateView, "Channel ID must not be blank!");
            dialogUpdateView.getTxtChannelID().requestFocus();
            return null;
        } 
        
        try {
            Integer.parseInt(channelID);
        } catch (NumberFormatException ex) {
            MessageDialogUtils.dialogError(
                    dialogUpdateView, "Channel ID must be numeric!");
            dialogUpdateView.getTxtChannelID().requestFocus();
            return null;
        }
        if (!channelDao.checkIDExist(channelID)) {
            MessageDialogUtils.dialogError(dialogUpdateView, 
                    "No channel found with ID: " + channelID + "! Please choose another ID");
            dialogUpdateView.getTxtChannelID().requestFocus();
            return null;
        }
        String campaignName = dialogUpdateView.getTxtCampaignName().getText();
        if (campaignName.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogUpdateView, "Campaign Name must not be blank!");
            dialogUpdateView.getTxtCampaignName().requestFocus();
            return null;
        } else {
            if (campaignName.length()>50) {
                MessageDialogUtils.dialogError(
                    dialogUpdateView, "Campaign Name must be maximum of 50 characters!");
                dialogUpdateView.getTxtCampaignName().requestFocus();
                return null;
            }
        }
        
        String promotionID = dialogUpdateView.getTxtPromotionID().getText();
        if (promotionID.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogUpdateView, "Promotion ID must not be blank!");
            dialogUpdateView.getTxtPromotionID().requestFocus();
            return null;
        } 
        
        try {
            Integer.parseInt(promotionID);
        } catch (NumberFormatException ex) {
            MessageDialogUtils.dialogError(
                    dialogUpdateView, "Promotion ID must be numeric!");
            dialogUpdateView.getTxtPromotionID().requestFocus();
            return null;
        }
        
        if (!promotionDao.checkIDExist(promotionID)) {
            MessageDialogUtils.dialogError(dialogUpdateView, 
                    "No promotion found with ID: " + promotionID + "! Please choose another ID");
            dialogUpdateView.getTxtPromotionID().requestFocus();
            return null;
        }
        
        Date startDate;
        try {
            startDate= validateDate(dialogUpdateView.getTxtStartDate().getText());
        } catch (ParseException ex) {
            MessageDialogUtils.dialogError(dialogUpdateView, "Start date must be in format dd/mm/yyyy!");
            dialogUpdateView.getTxtStartDate().requestFocus();
            return null;
        }
        Date endDate;
        try {
            endDate= validateDate(dialogUpdateView.getTxtEndDate().getText());
        } catch (ParseException ex) {
            MessageDialogUtils.dialogError(dialogUpdateView, "End date must be in format dd/mm/yyyy!");
            dialogUpdateView.getTxtEndDate().requestFocus();
            return null;
        }
        
        if (startDate!=null && endDate !=null && startDate.after(endDate)) {
            MessageDialogUtils.dialogError(dialogUpdateView, "Start date must be <= end date!");
            dialogUpdateView.getTxtStartDate().requestFocus();
            return null;
        }
        
        
        String productID = dialogUpdateView.getTxtProductID().getText();
        if (productID.isEmpty()) {
            MessageDialogUtils.dialogError(
                    dialogUpdateView, "Product ID must not be blank!");
            dialogUpdateView.getTxtProductID().requestFocus();
            return null;
        } else {
            if (!productDao.checkIDExist(productID)) {
                MessageDialogUtils.dialogError(dialogUpdateView, 
                        "No product found with ID: " + productID + "! Please choose another ID");
                dialogUpdateView.getTxtProductID().requestFocus();
                return null;
            }
        }
        
        Campaign campaign = new Campaign(campaignID, campaignName, 
                0, 0, 0, 0, 
                startDate, endDate);
        campaign.setChannel(new Channel(Integer.parseInt(channelID), ""));
        campaign.setPromotion(
                new Promotion(Integer.parseInt(promotionID), 
                0, ""));
        campaign.setProduct(new Product(productID, "", "", null));
        return campaign;
    }
    
    private Date validateDate(String date) throws ParseException {
        Date dateRet = null;

        if (!date.isEmpty()) {
            if (date.trim().length()!=10) {
                throw new ParseException("Date parse exception", 0);
            }
            Matcher matcher = pattern.matcher(date.trim());
            if (!matcher.matches()) {
                throw new ParseException("Date parse exception", 0);
            }
            dateRet = Utils.parseDate(date.trim(),
                    CisConstants.DATE_DD_MM_YYYY);
        }
        return dateRet;
    }
}
